package com.example.controller;

import com.example.common.Result;
import com.example.entity.Employee;
import com.example.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/employee")

public class EmployeeController {
    @Resource // 自动注入
    private EmployeeService employeeService;

    /**
     * 新增数据
     */
    @PostMapping("/add")
    public Result add(@RequestBody Employee employee) {
        employeeService.add(employee);
        return Result.success(null);
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete")
    public Result deleteBatch(@RequestParam String ids) {
        employeeService.delete(ids);
        return Result.success(null);
    }
    /**
     * 更新数据
     */
    @PutMapping("/update")
    public Result update(@RequestBody Employee employee) {
        employeeService.update(employee);
        return Result.success(null);
    }


    //    分页查询
    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             @RequestParam(required = false) Long id,
                             @RequestParam(required = false) String name) {
        PageInfo<Employee> pageInfo = employeeService.selectPage(pageNum, pageSize, id, name);
        return Result.success(pageInfo);
    }

}
